package com.happyrecyclingstation.service.impl;

import cn.hutool.crypto.digest.BCrypt;
import com.happyrecyclingstation.constant.JwtConstant;
import com.happyrecyclingstation.constant.MessageConstant;
import com.happyrecyclingstation.dto.LoginDto;
import com.happyrecyclingstation.entity.User;
import com.happyrecyclingstation.enumeration.UserType;
import com.happyrecyclingstation.exception.LoginException;
import com.happyrecyclingstation.mapper.UserMapper;
import com.happyrecyclingstation.properties.JwtProperties;
import com.happyrecyclingstation.service.LoginService;
import com.happyrecyclingstation.utils.JwtUtils;
import com.happyrecyclingstation.vo.LoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     * @param loginDto
     * @return
     */
    @Override
    public LoginVO login(LoginDto loginDto) {
        String phone = loginDto.getPhone();
        String password = loginDto.getPassword();
        User user = userMapper.selectByPhone(phone);
        // 电话号码校验
        if(user == null){
            throw new LoginException(MessageConstant.USERNAME_PASSWORD_ERROR);
        }
        // 密码校验
        if(!BCrypt.checkpw(password, user.getPassword())){
            throw new LoginException(MessageConstant.USERNAME_PASSWORD_ERROR);
        }
        // 用户状态校验 0 禁用 1 正常
        if(user.getStatus() == 0){
            throw new LoginException(MessageConstant.ACCOUNT_DISABLED);
        }
        // 生成jwt令牌
        UserType userType = UserType.fromUserType(user.getUserType());
        Map<String, Object> claims = new HashMap<>();
        String token;
        switch (userType){
            case ADMIN:
                claims.put(JwtConstant.ADMIN_ID, user.getId());
                token = JwtUtils.generateToken(jwtProperties.getAdminSecretKey(),
                        jwtProperties.getAdminExpiration(), claims);
                break;
            case HR:
                claims.put(JwtConstant.HR_ID, user.getId());
                token = JwtUtils.generateToken(jwtProperties.getHrSecretKey(),
                        jwtProperties.getHrExpiration(), claims);
                break;
            case JOB_SEEKER:
                claims.put(JwtConstant.JOB_SEEKER_ID, user.getId());
                token = JwtUtils.generateToken(jwtProperties.getJobSeekerSecretKey(),
                        jwtProperties.getJobSeekerExpiration(), claims);
                break;
            default:
                throw new LoginException(MessageConstant.INVALID_USER_TYPE);
        }
        // 封装数据返回
        LoginVO loginVo = LoginVO.builder()
                .id(user.getId())
                .username(user.getUsername())
                .userType(user.getUserType())
                .token(token)
                .build();
        return loginVo;
    }
}
